import 'package:sleep/help_style.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';

class FatigueStateChart extends StatelessWidget {
  final List<List<FlSpot>>? spots;

  final double aspectRatio;
  final double? maxX;
  final double? minX;
  final double? maxY;
  final double? minY;
  final double mimResolution;

  FatigueStateChart(
      {Key? key,
      this.spots,
      required this.aspectRatio,
      this.maxX,
      this.minX,
      this.maxY,
      required this.mimResolution,
      this.minY})
      : super(key: key);

  List<Color> gradientColors = [
    Colors.green,
    Colors.green,
  ];

  final mainGridLineColor = Colors.blue;
  @override
  Widget build(BuildContext context) {
    return AspectRatio(
      aspectRatio: aspectRatio,
      child: LineChart(
        mainData(),
        swapAnimationDuration: const Duration(milliseconds: 0),
      ),
    );
  }

  LineChartData mainData() {
    return LineChartData(
      gridData: FlGridData(
        show: true,
        drawVerticalLine: true,
        horizontalInterval: mimResolution,
      ),
      titlesData: FlTitlesData(
        show: true,
        rightTitles: AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        topTitles: AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        bottomTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: false,
            reservedSize: 30,
            interval: 1,
          ),
        ),
        leftTitles: AxisTitles(
          sideTitles: SideTitles(
              showTitles: true,
              interval: mimResolution,
              reservedSize: 40,
              getTitlesWidget: leftTitleWidgets),
        ),
      ),
      borderData: FlBorderData(
        show: true,
        border: Border.all(color: const Color(0xff37434d)),
      ),
      minX: minX,
      maxX: maxX,
      minY: minY,
      maxY: maxY,
      lineBarsData: datas(spots),
    );
  }

  List<LineChartBarData>? datas(List<List<FlSpot>>? spotses) {
    if (spotses == null) return [];
    final sp = spotses.map((e) {
      if (e.isEmpty) {
        return LineChartBarData(
          spots: e,
          isCurved: false,
          // color: colorsMap[first],
          barWidth: 1.5,
          isStrokeCapRound: false,
          dotData: FlDotData(
            show: false,
          ),
        );
      }
      final first = e.first;
      return LineChartBarData(
        spots: e,
        isCurved: true,
        color: colorsMap[first],
        barWidth: 1.5,
        isStrokeCapRound: true,
        dotData: FlDotData(
          show: false,
        ),
      );
    }).toList();

    return sp;
  }

  Map titleMap = {
    0: 'Normal',
    1: 'Mild',
    2: 'Moderate',
    3: 'Severe',
  };

  Map colorsMap = {
    0: Colors.red,
    1: Colors.green,
    2: Colors.black,
    3: Colors.blue,
  };

  Widget leftTitleWidgets(double value, TitleMeta meta) {
    const style = TextStyle(
        fontWeight: FontWeight.bold, fontSize: 8, color: Colors.white);
    String text = titleMap[value.toInt()];

    return Text(text, style: style, textAlign: TextAlign.center);
  }
}
